//加载事件
$(function (){
    indexInit();
});

//初始化轮播图，商品
function indexInit(){
    $.get(
        "indexInit",
        function (msg){
            //得到轮播图
           let ads=msg.data.ads;
            //循环
            for (let i = 0; i < ads.length; i++) {
                let tr=$(`<div class="carousel-item">
                                    <img src="${ads[i].img}" class="img-fluid" title="${ads[i].remark}" />
                                    <div class="carousel-caption">
                                    <h4>${ads[i].title}</h4>
                                    </div>
                                </div>`);
                //默认选中第一个
                if(i==0){
                    tr.addClass("active");
                }
                //追加到id为gg-div的div里面
                $("#gg-div").append(tr);
            }

            //得到商品
            let products=msg.data.products;
            //循环这个商品
            for (let i = 0; i < products.length; i++) {
                let ptr=$(`
                        
                            <div class="col-sm-3 product-item">
                                <div class="img-thumbnail">
                                    <div>
                                    <a href="product.html?id=${products[i].id}">
                                        <img src="${products[i].url}" class="img-fluid rounded" />
                                    </a>
                                    </div>
                                </div>
                                <div class="title">
                                    <a href="products.html">${products[i].name}</a>
                                </div>
                                <div class="price">
                                    <span>￥</span>
                                    <span>${products[i].price}</span>
                                </div>
                      </div>`);
                //追加到id为sp-div的div里面
                $("#sp-div").append(ptr);
            }
        }
    )
}